// autocomplete library
$(function(){
	$('.autocomplete-container').each(function(){
		new Autocomplete($(this));
	});
	
	$(document.body).click(function(){
		$('.autocomplete-list').hide();
		$('.autocomplete-search').removeClass('focus');
	});
})

function Autocomplete(element) {
	this.element = element;
	this.element.data("autocomplete", this);
	
	this.list = $('<ul class="autocomplete-list">');
	this.element.append(this.list);
	
	this.input_search = this.element.find('.autocomplete-search');
	this.input_value  = this.element.find('.autocomplete-value');
    this.url = this.input_search.attr("ac-url");
	this.onselect = this.input_search.attr("ac-select");
    
    this.reset = this.element.find('.reset');
    if(this.input_value.val().length==0) {
        this.reset.hide();
    }
    this.reset.data('autocomplete', this);
    this.reset.click(function(){
        var ac = $(this).data('autocomplete');
        ac.input_value.val('');
        ac.element.find('.autocomplete-label').html('');
        $(this).hide();
    });
	
	this.input_search.click(function(){
		return false;
	});
	this.input_search.focus(function(){
		$(this).addClass('focus');
	});
	
	this.input_search.keyup(function(){
		var _this=$(this);
		$(this).attr("timeout", setTimeout(function(){
			var AC = _this.parent().data('autocomplete');
			var kword = _this.val().trim();
			
			if(kword.length>2){
				$.ajax({
					url: AC.url.replace("{kword}", kword),
					beforeSend: function() {
						AC.input_search.addClass('loading');
					},
					complete: function(req) {
						AC.input_search.removeClass('loading');
						if(req.status==200){
							var result = eval('('+req.responseText+')');
							var resultType = typeof(result.length);
							
							if(result.length>0 || resultType.toLowerCase()=="undefined"){
								var li;
								AC.list.empty();
								AC.list.data("ac", AC);
								for(id in result) {
									li = $('<li>').appendTo(AC.list);
									li.data("item", {value: id, label: result[id]});
									li.html(result[id]);
									li.click(function(){
										var item = $(this).data("item");
										var ul = $(this).parent();
										var AC = ul.data('ac');
										var container = ul.parent();
										container.find(".autocomplete-value").val(item.value);
										container.find(".autocomplete-label")
											.html(item.label)
											.attr('title', item.label);
										
										AC.reset.show();
										eval(AC.onselect + '("' + id + '" , "' + result[id] + '")');
										ul.hide();
									});
								}
								AC.list.show();
							}else{
								AC.list.hide();
							}
						}else{
							AC.list.hide();
						}
					}
				});
			}else{
				AC.list.hide();
			}
			
			if(to = $(_this).attr("timeout")){
				clearTimeout(to);
			}
		}, 800));
	});
	this.input_search.keydown(function(e){
		if(to = $(this).attr("timeout")){
			clearTimeout(to);
		}
		//disable ENTER key
		if(e.keyCode==13) {
			return false;
		}
	});
}